package com.zcoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcoj.core.model.PageData;
import com.zcoj.core.model.PageWrap;
import com.zcoj.mapper.JudgeResultMapper;
import com.zcoj.pojo.Dto.JudgeResult;
import com.zcoj.service.JudgeResultService;
import com.zcoj.util.WrapperUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Service实现
 * @author 剩xia的盛夏
 * @date 2023/04/12 10:17
 */
@Service
public class JudgeResultServiceImpl implements JudgeResultService {

    @Autowired
    private JudgeResultMapper judgeResultMapper;

    @Override
    public Integer create(JudgeResult judgeResult) {
        judgeResultMapper.insert(judgeResult);
        return judgeResult.getId();
    }

    @Override
    public void deleteById(Integer id) {
        judgeResultMapper.deleteById(id);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        judgeResultMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(JudgeResult judgeResult) {
        judgeResultMapper.updateById(judgeResult);
    }

    @Override
    public void updateByIdInBatch(List<JudgeResult> judgeResults) {
        if (CollectionUtils.isEmpty(judgeResults)) return;
        for (JudgeResult judgeResult: judgeResults) {
            this.updateById(judgeResult);
        }
    }

    @Override
    public JudgeResult findById(Integer id) {
        return judgeResultMapper.selectById(id);
    }

    @Override
    public JudgeResult findOne(JudgeResult judgeResult) {
        Wrapper<JudgeResult> wrapper = new QueryWrapper<>(judgeResult);
        return judgeResultMapper.selectOne(wrapper);
    }

    @Override
    public List<JudgeResult> findList(JudgeResult judgeResult) {
        Wrapper<JudgeResult> wrapper = new QueryWrapper<>(judgeResult);
        return judgeResultMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<JudgeResult> findPage(PageWrap<JudgeResult> pageWrap) {
        IPage<JudgeResult> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<JudgeResult> queryWrapper = new QueryWrapper<>(WrapperUtil.blankToNull(pageWrap.getModel()));
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase("DESC")) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(judgeResultMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(JudgeResult judgeResult) {
        Wrapper<JudgeResult> wrapper = new QueryWrapper<>(judgeResult);
        return judgeResultMapper.selectCount(wrapper);
    }
}
